import React, {useEffect, useRef, useState} from "react";
import {Button, Form, Modal, Row, Toast} from "@douyinfe/semi-ui";
import {IconPlus} from "@douyinfe/semi-icons";
import {AddCollege} from "../../../../../api/api.js";

function College_AForm() {
    const [visible, setVisible] = useState(false);
    const [treeData, settreeData] = useState(null)
    const [list3, setlist3] = useState(0)
    useEffect(() => {
    }, [])

    function setter() {
        api.current.setValue("collegename", props.mess.cname)
    }

    const showDialog = () => {
        setVisible(true);
    };
    const handleOk = () => {
        setVisible(false);
    };
    const handleCancel = () => {
        setVisible(false);
    };
    const handleAfterClose = () => {
    };
    const style = {
        boxShadow: 'var(--semi-shadow-elevated)',
        backgroundColor: 'var(--semi-color-bg-2)',
        borderRadius: '4px',
        padding: '10px',
        marginRight: '20px',
        width: '600px',
    };
    const childRef = useRef();

    async function sub() {
        let form = new FormData()
        form.append("collegename",api.current.getValues().collegename)
        const res = await AddCollege(form);
        if (res.data.code==200){
            Toast.success('学院添加成功')
            handleOk()
        }else {
            Toast.success('学院添加失败')
            handleOk()
        }
    }
    function valte() {
        api.current.validate()
            .then(values=>{
                sub()
            })
            .catch(errors=>{
                Toast.error("请重新填写表单")
            })
    }

    const api = useRef();

    return (
        <>
            <Button onClick={showDialog} icon={<IconPlus/>} theme="solid">添加学院</Button>
            <Modal
                okText={'Sounds great!'}
                style={{width: "20%", height: "25%"}}
                title={<div><IconPlus style={{marginRight: "10px"}}/>添加学院</div>}
                visible={visible}
                onOk={handleOk}
                afterClose={handleAfterClose} //>=1.16.0
                onCancel={handleCancel}
                closeOnEsc={true}
                footer={
                    <div style={{textAlign: 'center'}}>
                        <Button type="primary" onClick={() => {
                            valte()
                        }}>
                            提交
                        </Button>
                    </div>

                }
            >

                <Row style={{display: 'flex',justifyContent:'center'}}>
                    <Form
                        getFormApi={formApi => api.current = formApi}
                        onSubmit={values => Toast.info({content: JSON.stringify(values)})}
                    >
                        <>

                            <Form.Input
                                rules={[
                                    { required: true, message: '' },
                                    { type: 'string', message: 'type error' },
                                    { validator: (rule, value) => value != null, message: '学院名不能为空' },
                                ]}
                                style={{justifyContent:'center'}} label={'学院名'} field='collegename'
                                        placeholder='请输入学院名'/>
                        </>

                    </Form>
                </Row>
            </Modal>
        </>
    );
}

export default College_AForm